package pw.hais.etgsh.ui;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.squareup.okhttp.Response;

import java.math.BigDecimal;

import pw.hais.app.AppBasePopupWindow;
import pw.hais.etgsh.R;
import pw.hais.etgsh.model.CheckAPPModel;
import pw.hais.http.Http;
import pw.hais.http.base.OnHttpListener;
import pw.hais.http.image.CacheManager;
import pw.hais.utils.ApkInfoUtil;
import pw.hais.utils.EmptyUtil;

/**
 * Created by Hais1992 on 2015/12/18.
 */
public class UpdateAppWindows extends AppBasePopupWindow {
    private TextView text_msg, text_title;
    private Button btn_clean, btn_update;
    private CheckAPPModel checkAPPModel;
    private ProgressBar progressBar_down;


    public UpdateAppWindows(Activity activity) {
        super(activity, R.layout.windows_update_app);
    }

    @Override
    protected void drawEnd(View popupWindowView) {
        text_title = (TextView) popupWindowView.findViewById(R.id.text_title);
        text_msg = (TextView) popupWindowView.findViewById(R.id.text_msg);
        btn_clean = (Button) popupWindowView.findViewById(R.id.btn_clean);
        btn_update = (Button) popupWindowView.findViewById(R.id.btn_update);

        progressBar_down = (ProgressBar) popupWindowView.findViewById(R.id.progressBar_down);

        btn_clean.setOnClickListener(this);
        btn_update.setOnClickListener(this);
    }

    public void showCenter(CheckAPPModel checkAPPModel) {
        this.checkAPPModel = checkAPPModel;
        double size = checkAPPModel.File.metaData.size / 1024000;
        BigDecimal bigDecimal = new BigDecimal(size);
        size = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        text_title.setText(checkAPPModel.name);

        if(EmptyUtil.emptyOfString(checkAPPModel.message))checkAPPModel.message = "...";

        text_msg.setText("您需要从 " + ApkInfoUtil.getVersionName()
                + " 版升级到 " + checkAPPModel.version + " 才能获得更好的体验。\n\n软件升级改善了：\n" +checkAPPModel.message+
                "\n\n您是否要升级你的软件？\n\n\t●\t" + checkAPPModel.name + "　" + checkAPPModel.version + "　" + size + "MB");

        if(checkAPPModel.message!=null && !"".equals(checkAPPModel.message)){
            text_msg.append("\n\n更新介绍："+checkAPPModel.message);
        }

        super.showCenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.btn_clean:
                dismiss();
                break;
            case R.id.btn_update:
                btn_update.setEnabled(false);
                progressBar_down.setVisibility(View.VISIBLE);
                progressBar_down.setMax((int) checkAPPModel.File.metaData.size);
                Http.download(checkAPPModel.File.url, CacheManager.CACHE_DIR, new OnHttpListener<String>() {
                    @Override
                    public void onSuccess(Response response, String data) {
                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        i.setDataAndType(Uri.parse("file://" + data), "application/vnd.android.package-archive");
                        activity.startActivity(i);
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }

                    @Override
                    public void onProgress(long countLength,int progress) {
                        super.onProgress(countLength,progress);
                        progressBar_down.setProgress(progress);
                    }
                });
                break;
        }
    }
}
